﻿using System;
using SharpSTS.CardControl;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Net;
using System.Web.Caching;

namespace SharpSTS.Tests
{
    
    
    /// <summary>
    ///This is a test class for IPAddressAuthorisationTest and is intended
    ///to contain all IPAddressAuthorisationTest Unit Tests
    ///</summary>
    [TestClass()]
    public class IPAddressAuthorisationTest
    {


        private TestContext testContextInstance;

        /// <summary>
        ///Gets or sets the test context which provides
        ///information about and functionality for the current test run.
        ///</summary>
        public TestContext TestContext
        {
            get
            {
                return testContextInstance;
            }
            set
            {
                testContextInstance = value;
            }
        }

        #region Additional test attributes
        // 
        //You can use the following additional attributes as you write your tests:
        //
        //Use ClassInitialize to run code before running the first test in the class
        //[ClassInitialize()]
        //public static void MyClassInitialize(TestContext testContext)
        //{
        //}
        //
        //Use ClassCleanup to run code after all tests in a class have run
        //[ClassCleanup()]
        //public static void MyClassCleanup()
        //{
        //}
        //
        //Use TestInitialize to run code before running each test
        //[TestInitialize()]
        //public void MyTestInitialize()
        //{
        //}
        //
        //Use TestCleanup to run code after each test has run
        //[TestCleanup()]
        //public void MyTestCleanup()
        //{
        //}
        //
        #endregion


        #region Netmask parsing tests
        /// <summary>
        /// A test for ParseNetmask using a null netmask.
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException), "netmask")]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void ParseNullNetmaskTest()
        {
            string netmask = null;
            byte[] expected = { 255, 255, 255, 255 };
            byte[] actual;
            actual = IPAddressAuthorisation_Accessor.ParseNetmask(netmask);
        }

        /// <summary>
        /// A test for ParseNetmask using dotted quad netmasks.
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void ParseSimpleNetmaskTest()
        {
            string netmask = "255.255.255.255";
            byte[] expected = { 255, 255, 255, 255 };
            byte[] actual;
            actual = IPAddressAuthorisation_Accessor.ParseNetmask(netmask);

            for (int i = 0; i < expected.GetUpperBound(0); i++)
                Assert.AreEqual(expected[i], actual[i]);
        }

        #endregion

        /// <summary>
        ///A test for Parse
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void ParseTest()
        {
            string subnetString = "127.0.0.1/255.255.255.255";
            byte[] subnetActual;
            byte[] subnetExpected = { 127, 0, 0, 1 };
            byte[] netmaskActual;
            byte[] netmaskExpected = { 255, 255, 255, 255 };
            IPAddressAuthorisation_Accessor.Parse(subnetString, out subnetActual, out netmaskActual);
            for (int i = 0; i < subnetExpected.GetUpperBound(0); i++)
                Assert.AreEqual(subnetExpected[i], subnetActual[i]);
            for (int i = 0; i < netmaskExpected.GetUpperBound(0); i++)
                Assert.AreEqual(netmaskExpected[i], netmaskActual[i]);
        }


        /// <summary>
        ///A test for Contains
        ///</summary>
        [TestMethod()]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void ContainsTest()
        {
            IPAddress ip = new IPAddress(new byte[] { 10, 1, 0, 1 });
            string subnet = "10.0.0.0/255.0.0.0";
            bool expected = true;
            bool actual;
            actual = IPAddressAuthorisation_Accessor.Contains(ip, subnet);
            Assert.AreEqual(expected, actual);

            subnet = "10.0.0.0./255.255.255.0";
            expected = false;
            actual = IPAddressAuthorisation_Accessor.Contains(ip, subnet);
            Assert.AreEqual(expected, actual);
        }

        /// <summary>
        ///A test for Contains with a null ip
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException), "ip")]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void ContainsNullIPTest()
        {
            IPAddressAuthorisation_Accessor.Contains(null, "255.255.255.255");
        }

        /// <summary>
        ///A test for Contains with a null subnet
        ///</summary>
        [TestMethod()]
        [ExpectedException(typeof(ArgumentNullException), "subnet")]
        [DeploymentItem("SharpSTS.Core.dll")]
        public void ContainsNullSubnetTest()
        {
            IPAddressAuthorisation_Accessor.Contains(new IPAddress(new byte[] { 10, 1, 0, 1 }), null);
        }
    }
}
